//
//  MenuController.m
//  This file is part of D&D Manager
//
//  Created by Matthew Hutton on 21/03/2007.
//  Copyright 2007 Matthew Hutton.
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import "MenuController.h"
#import "MyDocument.h"

@implementation MenuController
-(id)init{
	[super init];
	NSUserDefaults *userDef=[NSUserDefaults standardUserDefaults];
	if([userDef objectForKey:@"showFriends"]==nil){
		[userDef setBool:YES
				  forKey:@"showFriends"];
	}
	if([userDef objectForKey:@"showEnemies"]==nil){
		[userDef setBool:YES
				  forKey:@"showEnemies"];
	}
	if([userDef objectForKey:@"characterClasses"]==nil){
		[userDef setObject:[NSArray arrayWithObjects:@"Bard",@"Barbarian",@"Cleric",@"Druid",@"Fighter",@"Monk",@"Paladin",@"Ranger",@"Rouge",@"Sourcerer",@"Wizard",nil]
					forKey:@"characterClasses"];
	}
	if([userDef objectForKey:@"domains"]==nil){
		[userDef setObject:[NSArray arrayWithObjects:@"Air", @"Animal", @"Chaos", @"Death", @"Destruction", @"Earth", @"Evil", @"Fire", @"Good", @"Healing", @"Logic", @"Law", @"Luck", @"Magic", @"Plant", @"Protection", @"Strength", @"Sun", @"Travel", @"Trickery", @"War", @"Water",nil]
					forKey:@"domains"];
	}
	return self;
}
+(void)changeExtensionToCampaignFromPath:(NSString *)oldFilename
						   withDocument:(NSPersistentDocument *)doc{
		//close current document.
	[doc close];
	NSFileManager *fileMan=[NSFileManager defaultManager];
	NSRange range=[oldFilename rangeOfString:@".quest" options:NSBackwardsSearch];
	NSString *newFilename=[oldFilename substringToIndex:range.location];
	newFilename=[NSString stringWithFormat:@"%@.campaign",newFilename];
	[fileMan copyPath:oldFilename toPath:newFilename handler:nil];
	NSError *error=nil;
	[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL URLWithString:newFilename] display:YES error:&error];
}
-(IBAction)displayLogSheet:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] displayLogSheet];
}
-(IBAction)rollDice:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] displayRollDiceSheet:nil];
}
-(IBAction)grantXP:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] displayGrantXPsheet:nil];
}
-(IBAction)import:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] importCharactersFromOtherFiles:nil];
}
-(IBAction)attack:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] runAttackFromMainScreen:nil];
}
-(IBAction)castSpell:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] castSpellFromMainScreen:nil];
}
-(IBAction)rest:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] loadRestScreen:nil];
}
-(IBAction)startNewBattle:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] startBattle:nil];
}
-(IBAction)continueBattle:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] continueBattle:nil];
}
-(IBAction)exitBattle:(id)sender{
	MyDocument *currentDocument=(MyDocument *)[[NSDocumentController sharedDocumentController] currentDocument];
	if(currentDocument==nil){
		currentDocument=(MyDocument *)[[NSApp orderedDocuments] objectAtIndex:0];
		if(currentDocument==nil){
			NSLog(@"currentDocument playing up, and order docs playing up too.");
			NSBeep();
			return;
		}
	}
	[[currentDocument appController] exitBattle:nil];
}
-(IBAction)showPreferences:(id)sender{
	[characterClassTable reloadData];
	[domainsTable reloadData];
	[preferencesPanel makeKeyAndOrderFront:sender];
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender{
	//check if battlescreen is visible.
	NSArray *docs=[[NSDocumentController sharedDocumentController] documents];
	int i;
	for(i=0;i<[docs count];i++){
		MyDocument *myDoc=(MyDocument *)[docs objectAtIndex:i];
		AppController *appCont=[myDoc appController];
		if([[NSUserDefaults standardUserDefaults] boolForKey:@"clearLogOnExit"]){
			[appCont clearLogSheet:self];
		}
		NSWindow *battleScreen=[[appCont battleController] battleScreen];
		if([battleScreen isVisible]){
			[battleScreen performClose:self];
		}
	}
	return YES;
}
-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
	if([aTableView isEqualTo:characterClassTable]){
		return [[[NSUserDefaults standardUserDefaults] stringArrayForKey:@"characterClasses"] count];
	}else if([aTableView isEqualTo:domainsTable]){
		return [[[NSUserDefaults standardUserDefaults] stringArrayForKey:@"domains"] count];
	}
	return 0;
}
-(id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
		   row:(int)row{
	if([aTableView isEqualTo:characterClassTable]){
		NSString *str=[[[NSUserDefaults standardUserDefaults] stringArrayForKey:@"characterClasses"] objectAtIndex:row];
		NSLog(@"str=%@",str);
		return str;
	}else if([aTableView isEqualTo:domainsTable]){
		return [[[NSUserDefaults standardUserDefaults] stringArrayForKey:@"domains"] objectAtIndex:row];
	}
	return nil;
}
@end

